package com.dingniu8;

import java.net.*;
import java.util.Arrays;
import java.io.*;

import com.tidemark.ResultTable;
import com.tidemark.Stock;
import com.tidemark.StockDataModel;


/**
 * reader provides go() method 
 * @author guest
 *
 */
public class Reader implements com.tidemark.Reader {
	


	/**
	 * 打印前7个表(都是主力增仓)中出现最多的股票，提示牛
	 * 打印第8个表(是资金流出表)中出现最多的股票，提示熊
	 * 
	 */
	public void propose() {

		ResultTable rts[][] = read(Constants.url);
		
		if(rts == null){
			return;
		}
		
		//merge rts[0~6][0-4] tables into one table, choose top N good  
		ResultTable good = 
			new ResultTable((Constants.NUM_TABLE_GROUP-1)*Constants.NUM_TABLE_TAB*Constants.NUM_TABLE_CELL);
		for(int i=0; i<good.table.length; i++){
			int g = i/(Constants.NUM_TABLE_TAB*Constants.NUM_TABLE_CELL);
			int t = (i%(Constants.NUM_TABLE_TAB*Constants.NUM_TABLE_CELL))/Constants.NUM_TABLE_CELL;
			int c = (i%(Constants.NUM_TABLE_TAB*Constants.NUM_TABLE_CELL))%Constants.NUM_TABLE_CELL;
			good.table[i] = rts[g][t].table[c];
			//System.out.println(good.table[i].getCode());
		}
		//System.out.println(":)   :)   :)   dingniu8.com (only Shanhai) ");
		sort_print(good, Constants.GOOD , "牛");		

		
		//merge rts[7][0-4] tables into one table, choose top N bad  
		ResultTable bad = 
			new ResultTable((1)*Constants.NUM_TABLE_TAB*Constants.NUM_TABLE_CELL);
		for(int i=0; i<bad.table.length; i++){
			int t = (i%(Constants.NUM_TABLE_TAB*Constants.NUM_TABLE_CELL))/Constants.NUM_TABLE_CELL;
			int c = (i%(Constants.NUM_TABLE_TAB*Constants.NUM_TABLE_CELL))%Constants.NUM_TABLE_CELL;
			bad.table[i] = rts[7][t].table[c];
			//System.out.println(t+" "+c);
		}		
		//System.out.println(":(   :(   :(   dingniu8.com (only Shanghai)");
		sort_print(bad, Constants.BAD, "熊");

	}

	/**
	 * sort the give table and print the top appears
	 * @param rts the array
	 * @param min_appears the times that the element appears in the array at least 
	 */
	private void sort_print(ResultTable rts, int min_appears, String comment){
		Arrays.sort(rts.table);
		Stock last = null;
		int counter = 0;

		for(int i=0 ; i<rts.table.length; i++){
			if(last != null && last.compareTo(rts.table[i]) == 0){
				counter++;
			}
			else{
				if(last != null && counter >= min_appears ){
//					System.out.print(lastcode + " ");
//					System.out.print(lastname + " ");
//					System.out.print(counter + comment + ", ");
					last.setComments(counter + comment);
					model.addStock(last);
				}
				last = rts.table[i];
				counter = 1;
			}
		}		
		
		model.addSeparator();
		//System.out.println();System.out.println();
	}
	
	
	/**
	 * read dingniu8 home page, convert all the html-tables into ResultTable
	 *  
	 * @param queryUrl
	 * @return
	 */
	ResultTable[][] read(String queryUrl) {
		try {
			URL url = new URL(queryUrl);
			BufferedReader in = new BufferedReader(new InputStreamReader(url
					.openStream(), "gb2312"));
			String str;

			ResultTable rts[][] = new ResultTable[Constants.NUM_TABLE_GROUP][Constants.NUM_TABLE_TAB];
			int i = 0;

			while ((str = in.readLine()) != null) {
				// System.out.println(str);
				ResultTable rt = parser.parse(str);
				if (null != rt) {
					rts[i / Constants.NUM_TABLE_TAB][i % Constants.NUM_TABLE_TAB] = rt;
					i++;
				}
			}

			in.close();

			if (i != Constants.NUM_TABLE_GROUP * Constants.NUM_TABLE_TAB) {
				System.out.println("Warning: something wrong... " + i);
				return null;
			} else {
				//System.out.println("dingniu8 parse page success!");
				return rts;
			}
		}

		catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}
	
	private Parser parser = new Parser();
	private StockDataModel model;
	
	public Reader(StockDataModel model){
		if(model == null){
			throw new NullPointerException("stock data model must not be null.");
		}
		this.model = model;
	}
	
}
